/**
 * Copyright (C) 2009 Ignasi Fosch Alonso
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 
 * details.
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA 02111-1307 USA
 *
 * You can reach me by mail at natx@y10k.ws
 * You can reach him by paper mail at:
 * C. LLEIDA, 41-43, 3r 1a
 * El Prat de Llobregat 08820
 * Barcelona, SPAIN
 */
package ws.y10k.goloim.interfaces.swt;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.TableItem;

import ws.y10k.goloim.model.Pallet;
import ws.y10k.goloim.model.PalletAlreadyExistsException;
import ws.y10k.goloim.model.PalletDoesNotExistException;

/**
 * @author natx
 *
 */
public class SavePalletSelectionListener implements SelectionListener {
	
	private ManagePalletsWindow parent;
	
	public SavePalletSelectionListener(ManagePalletsWindow parent) {
		this.parent = parent;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent)
	 */
	@Override
	public void widgetDefaultSelected(SelectionEvent arg0) { }

	/* (non-Javadoc)
	 * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
	 */
	@Override
	public void widgetSelected(SelectionEvent arg0) {
		// TODO i18n
		if ( parent.txtCode.getText().length() > 0 ) {
			
			String code = parent.txtCode.getText();
			String description = parent.txtDescription.getText();
			double width;
			try {
				if ( parent.txtWidth.getText().length() > 0 )	width = Double.parseDouble(parent.txtWidth.getText());
				else width = 0;
			} catch (NumberFormatException e) {
				MessageDialog.openError(parent.getShell(), "Format error in width field", "Width field must be a number.");
				return;
			}
			double height;
			try {
				if ( parent.txtHeight.getText().length() > 0 )	height = Double.parseDouble(parent.txtHeight.getText());
				else height = 0;
			} catch (NumberFormatException e) {
				MessageDialog.openError(parent.getShell(), "Format error in height field", "Height field must be a number.");
				return;
			}
			double depth;
			try {
				if ( parent.txtDepth.getText().length() > 0 )	depth = Double.parseDouble(parent.txtDepth.getText());
				else depth = 0;
			} catch (NumberFormatException e) {
				MessageDialog.openError(parent.getShell(), "Format error in depth field", "Depth field must be a number.");
				return;
			}
			double weight;
			try {
				if ( parent.txtWeight.getText().length() > 0 )	weight = Double.parseDouble(parent.txtWeight.getText());
				else weight = 0;
			} catch (NumberFormatException e) {
				MessageDialog.openError(parent.getShell(), "Format error in weight field", "Weight field must be a number.");
				return;
			}
			try {
				Pallet pallet = new Pallet(code,description,width,height,depth,weight);

				if ( parent.itmSelected != null ) {
					try {
						Pallet oldPallet = GoLoIm.pallets.getPallet(parent.itmSelected.getText(0));
						oldPallet.setCode(code);
						oldPallet.setDescription(description);
						oldPallet.setWidth(width);
						oldPallet.setHeight(height);
						oldPallet.setDepth(depth);
						oldPallet.setWeight(weight);
						String[] values = {code,description,((Double) width).toString(),((Double) height).toString(),((Double) depth).toString(),((Double) weight).toString()};
						parent.itmSelected.setText(values);
						parent.itmSelected = null;
						parent.tblPallets.deselectAll();
					} catch (PalletDoesNotExistException e) { }
				} else {
					GoLoIm.pallets.add(pallet);
					TableItem itmBox = new TableItem(parent.tblPallets, SWT.NONE);
					itmBox.setText(0,pallet.getCode());
					itmBox.setText(1,pallet.getDescription());
					itmBox.setText(2,((Double) pallet.getWidth()).toString());
					itmBox.setText(3,((Double) pallet.getHeight()).toString());
					itmBox.setText(4,((Double) pallet.getDepth()).toString());
					itmBox.setText(5,((Double) pallet.getWeight()).toString());
				}
				parent.txtCode.setText("");
				parent.txtDescription.setText("");
				parent.txtWidth.setText("");
				parent.txtHeight.setText("");
				parent.txtDepth.setText("");
				parent.txtWeight.setText("");
				parent.txtCode.setFocus();
			} catch (PalletAlreadyExistsException e) {
				MessageDialog.openError(parent.getShell(), "Pallet already exists", "A pallet like that already exists.");
			}
		} else {
			MessageDialog.openError(parent.getShell(), "Not enough information", "You must specify code of the pallet.");
		}
		
	}

}
